<template>
	<div style="width: 100%" align="center">
		<h3>登录</h3>
		<table>
			<tr>
				<td>账号:</td>
				<td><input type="text" v-model="user.username" /></td>
			</tr>
			<tr>
				<td>密码:</td>
				<td><input type="text" v-model="user.password" /></td>
			</tr>
			<tr>
				<td></td>
				<td><button @click="login()">登录</button></td>
			</tr>
		</table>
	</div>
</template>

<script>
	export default {
		data() {
			return{
				user:{},
			}
		},
		methods:{
			login(){
				let fd = new FormData();
				fd.append("username",this.user.username);
				fd.append("password",this.user.password);
				
				this.$postx("/api/login",this.user
				).then((resp) => {
					
					console.log(resp.data.data)
					//
					this.user.token = resp.data.data.token
					window.localStorage.setItem('token',resp.data.data.token);
					
					this.$get("/api/user/info"
					).then((resp) => {
						console.log(resp.data.data)
						
						this.$store.state.user = resp.data.data;
						
						window.localStorage.setItem("uname",resp.data.data.uname);
						window.localStorage.setItem("role",resp.data.data.role);
						window.localStorage.setItem("pic",resp.data.data.pic);
						this.user.token = window.localStorage.getItem("token");
						
						
						console.log("user对象")
						console.log(this.$store.state.user)

						this.$router.push("/" + this.$store.state.user.role)
						this.$store.state.erropen = true;
						this.$store.state.errmsg = "登录成功";
						
						})
				})
				
			},
		},
		mounted() {
			
		}
	}
</script>

<style>
</style>
